package com.yxt.controller;


import static yxt.util.Util.jsonp;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yxt.model.ChatHis;
import com.yxt.service.MessageService;
import com.yxt.service.UserService;

@Controller
@RequestMapping("message")
public class Message {
	@Autowired
	private MessageService msgService;
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/getAllMsgList.do",method=RequestMethod.GET)
	@ResponseBody 
	public Map<String,Object> getAllMsgList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> rtn = new HashMap<String,Object>();
		String query = request.getParameter("query");
		if(query != null) {
			query = new String(query.getBytes("ISO8859_1"), "UTF-8");
		}
		Map<String,Object> p = new HashMap<String,Object>();
		p.put("query", query);
		List<ChatHis> chatHisList = msgService.getAllMsgList(p);
		rtn.put("data", chatHisList);
		return jsonp(rtn, request, response);
	}
	@RequestMapping(value="/send.do")
	@ResponseBody 
	public Map<String,Object> sendText(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> paramMap = new HashMap<String,Object>();
		request.setCharacterEncoding("UTF-8");
		Enumeration<String> pNames = request.getParameterNames();
		while(pNames.hasMoreElements()){
			String pName =pNames.nextElement();			
			paramMap.put(pName, request.getParameter(pName));
		}
		Date now = new Date();

		paramMap.put("chat_date", now);
		msgService.insertMsg(paramMap);
		return jsonp(paramMap, request, response);
	}
	
	@RequestMapping(value="/getNewMsgList.do")
	@ResponseBody
	public Map<String,Object> getNewMsgList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> paramMap = new HashMap<String,Object>();
		Map<String,Object> respMap = new HashMap<String,Object>();
		Enumeration<String> pNames = request.getParameterNames();
		
		while(pNames.hasMoreElements()){
			String pName =pNames.nextElement();			
			paramMap.put(pName, request.getParameter(pName));
		}
		if(paramMap.get("now") == null) {
			paramMap.put("now", new Date());
		}
		List<ChatHis> newMsgList;
		do {
			newMsgList = msgService.getNewMsgList(paramMap);
			Thread.currentThread().sleep(500);
		} while(newMsgList == null || newMsgList.size() == 0 );
		respMap.put("data", newMsgList);
		return jsonp(respMap, request, response);
	}
	@RequestMapping(value="/updateChatHis.do")
	@ResponseBody 
	public void updateChatHis(
			@RequestParam(value = "chat_id") int chat_id,
			@RequestParam(value = "text") String text
	) throws Exception{
		msgService.updateChatHis(chat_id, text);
	}
	
	@RequestMapping(value="/deleteChatHis.do")
	@ResponseBody 
	public void deleteChatHis(
			@RequestParam(value = "chat_id") int chat_id
	) throws Exception{
		msgService.deleteChatHis(chat_id);
	}
}
